using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Watermelon;

public class UpgradeUIPanel : MonoBehaviour
{
    private const string LEVEL = "LVL {0}";

    [SerializeField] Image iconImage;
    [SerializeField] Image buttonImage;
    [SerializeField] Sprite buttonActiveSprite;
    [SerializeField] Sprite buttonDisableSprite;

    [Space]
    [SerializeField] GameObject adsButtonObject;

    [Space]
    [SerializeField] GameObject maxObject;

    [Space]
    [SerializeField] Text priceText;
    [SerializeField] Text levelText;
    [SerializeField] Text nameText;

    private CanvasGroup canvasGroup;
    public CanvasGroup CanvasGroup => canvasGroup;

    private CanvasGroup priceCanvasGroup;

    private BaseUpgrade upgrade;
    public BaseUpgrade Upgrade => upgrade;

    private UISecretaryWindow uiSecretaryWindow;

    public void Initialise(UISecretaryWindow uiSecretaryWindow, BaseUpgrade upgrade)
    {
        this.uiSecretaryWindow = uiSecretaryWindow;
        this.upgrade = upgrade;

        // Get component
        canvasGroup = GetComponent<CanvasGroup>();
        priceCanvasGroup = priceText.GetComponent<CanvasGroup>();

        // Redraw panel
        Redraw();

        // Set name
        nameText.text = upgrade.Title;
        iconImage.sprite = upgrade.Icon;
    }

    public void Redraw()
    {
        // Doctor is opened
        if (upgrade.Upgrades.Length <= upgrade.UpgradeLevel + 1)
        {
            // Disable button
            buttonImage.gameObject.SetActive(false);
            adsButtonObject.gameObject.SetActive(false);

            // Enable hired panel
            maxObject.SetActive(true);

            // Reset panel transparent
            canvasGroup.alpha = 1.0f;
        }
        else
        {
            // Enable button
            buttonImage.gameObject.SetActive(true);
            adsButtonObject.gameObject.SetActive(true);

            // Enable hired panel
            maxObject.SetActive(false);

            int price = upgrade.Upgrades[upgrade.UpgradeLevel + 1].Price;
            if (CurrenciesController.HasAmount(Currency.Type.Money, price))
            {
                buttonImage.sprite = buttonActiveSprite;
                priceCanvasGroup.alpha = 1.0f;
            }
            else
            {
                buttonImage.sprite = buttonDisableSprite;
                priceCanvasGroup.alpha = 0.6f;
            }

            // Reset panel transparent
            canvasGroup.alpha = 1.0f;

            // Set price
            priceText.text = CurrenciesHelper.Format(price);
        }

        // Set level
        levelText.text = string.Format(LEVEL, upgrade.UpgradeLevel + 1);
    }

    public void PurchaseButton()
    {
        if (upgrade.Upgrades.Length >= upgrade.UpgradeLevel + 1)
        {
            int price = upgrade.Upgrades[upgrade.UpgradeLevel + 1].Price;
            if (CurrenciesController.HasAmount(Currency.Type.Money, price))
            {
                AudioController.PlaySound(AudioController.Sounds.buttonSound);

                CurrenciesController.Substract(Currency.Type.Money, price);

                upgrade.UpgradeStage();
            }
        }
    }

    public void PurchaseAdButton()
    {
        if (upgrade.Upgrades.Length >= upgrade.UpgradeLevel + 1)
        {
            AudioController.PlaySound(AudioController.Sounds.buttonSound);

            AdsManager.ShowRewardBasedVideo((bool reward) =>
            {
                if (reward)
                {
                    upgrade.UpgradeStage();
                }
            });
        }
    }
}
